home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_10_01 / cmenu.exe / RMENU4.C < prev    next >
Text File  |  1991-12-11  |  6KB  |  231 lines

  1. /************************************************************
  2.  *    Program: RMENU Menu Interpreter
  3.  *  Module: rmenu4.c
  4.  *        System-dependent Utility functions
  5.  *    Written by: Leor Zolman, 7/91
  6.  ************************************************************/
  7.  
  8. #include "cmenu.h"
  9. #include "rcmenu.h"
  10.  
  11. #if DOS
  12. #include <dir.h>
  13.  
  14. static struct {
  15.     int curdrive;
  16.     char curpath[MAX_PATH];
  17. } path_stack[MAX_PATH_STACK];
  18.  
  19. static int path_stackp = 0;
  20. #endif
  21.  
  22.  
  23. /************************************************************
  24.  * push_path()
  25.  *    DOS only: save drive and path for later restoration, and
  26.  *        select new drive based on new path
  27.  *  Unix: a no-op, since sub-shells cannot affect parent path
  28.  ************************************************************/
  29.  
  30. Void push_path()
  31. {
  32. #if DOS
  33.     if (path_stackp == MAX_PATH_STACK)
  34.         fatal("Maximum stack nesting reached");
  35.     else
  36.     {
  37.         path_stack[path_stackp].curdrive = getdisk();
  38.         getcwd(path_stack[path_stackp].curpath, MAX_PATH);
  39.         path_stackp++;
  40.     }
  41. #endif    
  42.     return;
  43. }
  44.  
  45.  
  46. /************************************************************
  47.  * pop_path()
  48.  *    DOS only: restore drive and path from path stack
  49.  *  Unix: a no-op, since sub-shells cannot affect parent path
  50.  ************************************************************/
  51.  
  52. Void pop_path()
  53. {
  54. #if DOS
  55.         path_stackp--;
  56.         setdisk(path_stack[path_stackp].curdrive);
  57.         chdir(path_stack[path_stackp].curpath);
  58. #endif    
  59.     return;
  60. }
  61.  
  62.  
  63. /************************************************************
  64.  * tty_shell()
  65.  *    Set tty mode for a shell call
  66.  ************************************************************/
  67.  
  68. Void tty_shell()
  69. {
  70. #if XENIX || (UNIX && OLD_CURSES)
  71.         resetty();
  72. #else
  73.         endwin();
  74. #endif
  75.     return;
  76. }
  77.  
  78.  
  79. /************************************************************
  80.  * tty_curses()
  81.  *    Set tty mode for curses
  82.  ************************************************************/
  83.  
  84. Void tty_curses()
  85. {                    /* set up tty modes        */
  86.     raw();            /* ignore ^C, etc.        */
  87.     nonl();
  88.     noecho();        /* do not echo input    */
  89.     keypad(stdscr, TRUE); /* enable keypad    */
  90.     return;
  91. }
  92.  
  93.  
  94. /************************************************************
  95.  * trans_slash():
  96.  *  Translate forward slashes (/) into backslashes (\) in
  97.  *  DOS pathnames.
  98.  ************************************************************/
  99.  
  100. static char *trans_slash(str)
  101. char *str;
  102. {
  103.     char *cptr;
  104.     
  105. #if DOS
  106.     for(cptr = str; *cptr; *cptr++)
  107.         if (*cptr == '/')
  108.             *cptr = '\\';
  109. #endif
  110.  
  111.     return str;
  112. }
  113.  
  114. /************************************************************
  115.  * make_path():
  116.  *    Construct new path based on old default path, old_path,
  117.  *    and current incremental path, incr_path (which might be
  118.  *    absolute)
  119.  ************************************************************/
  120.  
  121. char *make_path(old_path, incr_path)
  122. char *old_path, *incr_path;
  123. {
  124.     static char newpath[MAX_PATH];
  125.     
  126.     trans_slash(old_path);
  127.     trans_slash(incr_path);
  128.  
  129.     if (*incr_path == '/' || *incr_path == '\\') /* If path is absolute */
  130.         strcpy(newpath, incr_path);        /* then it is complete path    */
  131. #if DOS
  132.     else if (incr_path[1] == ':')        /* drive designator? if so,    */
  133.         strcpy(newpath, incr_path);        /* treat as ABSOLUTE path    */
  134. #endif
  135.     else
  136.     {
  137.         strcpy(newpath, old_path);            /* start with old path    */
  138.         if (*incr_path && *newpath)            /* add path delimiter    */
  139. #if DOS                                        /* (if necessary)        */
  140.             strcat(newpath, "\\");
  141. #else
  142.             strcat(newpath, "/");
  143. #endif
  144.         strcat(newpath, incr_path);            /* and append new path    */
  145.     }
  146.     return newpath;
  147. }
  148.  
  149.  
  150. /************************************************************
  151.  * make_cmd():
  152.  *    Construct a system command string based on a given path
  153.  *    and action string. If the path is non-null, generate a
  154.  *    leading "cd" command to change the current directory.
  155.  *    If running under DOS, then generate a drive selection
  156.  *    statement as well to change the current drive.
  157.  ************************************************************/
  158.  
  159. char *make_cmd(path, action)
  160. char *path, *action;
  161. {
  162.     static char cmd_line[MAX_CMD];
  163.     char strtemp[10];
  164.  
  165.     trans_slash(path);
  166.  
  167.     if (*path)        /* select new path */
  168.     {
  169.         sprintf(cmd_line, "cd %s; ", path);
  170. #if DOS
  171.         if (path[1] == ':')        /* select new drive */
  172.         {
  173.             sprintf(strtemp, "%c:; ", path[0]);
  174.             strcat(cmd_line, strtemp);
  175.         }
  176. #endif
  177.         strcat(cmd_line, action);    /* action text */
  178.     }
  179.     else            /* no new path: trivial case */
  180.         sprintf(cmd_line, "%s", action);
  181.  
  182.     return cmd_line;
  183. }
  184.  
  185.  
  186. /************************************************************
  187.  * system0(): Execute a command via a system() call.
  188.  *
  189.  *    If Unix, just pass the command string to a shell.
  190.  *
  191.  *    if DOS, pass each sub-command (delimited by ;) to
  192.  *            a command processor
  193.  ************************************************************/
  194.  
  195. #if UNIX || XENIX                /* Unix version trivial    */
  196.  
  197. int system0(cmd)
  198. char *cmd;
  199. {
  200.     return system(cmd);
  201. }
  202.  
  203. #endif                            /* end of Unix version    */
  204.  
  205. #if DOS
  206. int system0(cmd)
  207. char *cmd;
  208. {
  209.     char subcmd[MAX_CMD];
  210.     char savcmd[MAX_CMD], *cmdp;
  211.     int i, retval;
  212.     char *cp = savcmd;
  213.     char c, lastc;
  214.  
  215.     strcpy(savcmd, cmd);
  216.     while (*cp)                /* if more subcommands left */
  217.     {
  218.         if (*cp == ';')
  219.             cp++;
  220.         cmdp = cp;            /* ptr to start of command    */
  221.         while (*cp && *cp != ';')
  222.             cp++;            /* find end of subcommand    */
  223.         lastc = *cp;        /* save terminating char    */
  224.         *cp = '\0';            /* delimit subcommand        */
  225.         retval = system(cmdp);    /* pass it to system()    */
  226.         *cp = lastc;        /* restore last terminator    */
  227.     }
  228.     return retval;
  229. }
  230. #endif                        /* end of DOS version        */
  231.